from bson.objectid import ObjectId
from pymongo import MongoClient
from Cache import *

import re
import json
import gensim
import random
import difflib
import datetime
import jieba.posseg as pseg

# 缓存对象
cache = Cache()

# 省份-城市
prov_city = Prov_City()

client = MongoClient('127.0.0.1', 27017)

# 日志库
user_db = client['user_db']
user_log = user_db['user_log']

# 官网职位库
knx_posts_db = client['knx_posts_db']
offical_posts_coll = knx_posts_db['offical_posts_coll']

# boss直聘
knx_boss_db = client['knx_boss_db']
knx_boss_position = knx_boss_db['knx_boss_position']
knx_boss_enterprise = knx_boss_db['knx_boss_enterprise']

# 拉钩职位库
knx_lagou_db = client['knx_lagou_db']
knx_lagou_position = knx_lagou_db['knx_lagou_position']
knx_lagou_enterprise = knx_lagou_db['knx_lagou_enterprise']

# 大街职位库
knx_dajie_db = client['knx_dajie_db']
knx_dajie_position = knx_dajie_db['knx_dajie_position']
knx_dajie_enterprise = knx_dajie_db['knx_dajie_enterprise']

# 前程无忧职位库
knx_qiancheng_db = client['knx_qiancheng_db']
knx_qiancheng_position = knx_qiancheng_db['knx_qiancheng_position']
knx_qiancheng_enterprise = knx_qiancheng_db['knx_qiancheng_enterprise']

# 自导入职位库
knx_custom_db = client['knx_custom_db']
knx_custom_position = knx_custom_db['knx_custom_position']
knx_custom_enterprise = knx_custom_db['knx_custom_enterprise']

# 职位总库
knx_all_position_db = client['knx_all_position_db']
knx_all_position_coll = knx_all_position_db['knx_all_position_coll']

# 企业总库
knx_all_enterprise_db = client['knx_all_enterprise_db']
knx_all_enterprise_coll = knx_all_enterprise_db['knx_all_enterprise_coll']

model = gensim.models.doc2vec.Doc2Vec.load('../model_with_workers_1.mm')

# 企业库的映射
corps_map = {
    'qiancheng': knx_qiancheng_enterprise,
    'lagou': knx_boss_enterprise,
    'boss': knx_lagou_enterprise,
    'dajie': knx_dajie_enterprise,
    'knx':knx_custom_enterprise
}

# 职位库的映射
posts_map = {
    'qiancheng': knx_qiancheng_position,
    'offical': offical_posts_coll,
    'lagou': knx_boss_position,
    'boss': knx_lagou_position,
    'dajie': knx_dajie_position,
    'knx': knx_custom_position
}

# 停用词
with open('stopWords.txt', encoding = 'utf-8') as f:
    stoplist = [i.strip() for i in f.readlines()]

# 目标企业
with open('target_enterprise.txt', encoding = 'utf-8') as f:
    target_enterprise = [i.strip() for i in f.readlines()]

# 热门企业
with open('hot_enterprise.txt', encoding = 'utf-8') as f:
    hot_enterprise = [i.strip() for i in f.readlines()]

# 聚类结果
with open('clu_result_05_16', 'r', encoding = 'utf-8') as f:
    clu_result = json.load(f)
    clu_result = list(clu_result.values())

# 查询映射
with open('query_map.json', encoding = 'utf-8') as file:
    query_map = json.loads(file.read())

# 低端职位关键词
with open('shielding_words.txt', encoding = 'utf-8') as f:
    shielding_words = [i.strip() for i in f.readlines()]

with open('industry_map.json', encoding = 'utf-8') as file:
    industry_map = json.loads(file.read())

with open('functionals.json', encoding = 'utf-8') as file:
    functionals = json.loads(file.read())

with open('similar_map.json', encoding = 'utf-8') as file:
    similar_map = json.loads(file.read())

with open('synonymous_words.json', encoding = 'utf-8') as file:
    synonymous_words = json.loads(file.read())

    for sw in synonymous_words:
        for w in sw:
            similar_map[w.lower()] = {
                'words': [i.lower() for i in sw if i != w],
                'relation': 'synonymous'
            }


# 监测职位是否对工作经验有要求
def exp_require(description):
    if re.search("""必须[^；;.。，,、]+相关经验|[丰富的|深厚的].+经验|工作经验\d年|经验（\d-\d年）|经验[\d| |\-|:|：]+年|行业工作经验(者?)|资深.+行业经验|\d-\d年经验|相关工作经验.+优先考虑|[工作|行业|相关]?经验[^；;.。，,]+以上|经验(者?)优先|经验丰富|相关工作经验(者?)""", description):
        return True

    if re.search('[\d|一|二|两|三|四|五|六|七|八|九|十|\-| |半|多]+年[^；;.。，,、]+经验', description) or re.search('[有|(具备)|(具有)][^；;.。，,、]+?经验(者?)', description):
        return True

    return False


postition_memory_db = {}

# 将所有的职位数据库统一映射到一个空间中
for db in [knx_qiancheng_position, knx_lagou_position, knx_boss_position, knx_dajie_position, knx_custom_position]:
    for item in db.find({}):
        item['id'] = str(item['_id'])

        name = item['name'].lower()

        if not name:
            continue

        if not isinstance(item['location'], str):
            continue

        location = re.match('[^- ]+', item['location']).group() if re.match('[^- ]+', item['location']) else ''
        industry = item['industry']
        company = item['company']

        # 如果薪资并不是一个列表，那么在按照薪资搜索的时候回出问题，所以暂时先过滤
        if not isinstance(item['salary'], list):
            continue

        # 没有jd描述的过滤掉
        if not 'description' in item or not item['description']:
            continue

        # 过滤有经验要求的职位
        if exp_require(item['description']):
            continue

        # 学历过滤
        if '高中' in item['edu'] or '大专' in item['edu'] or '中专' in item['edu']:
            continue

        # 剔除各种过于高端的职位
        if '高级' in item['name'] or '资深' in item['name'] or '专家' in item['name']:
            continue

        # 剔除中高层管理职位
        if ('总监' in item['name'] or '主管' in item['name'] or '经理' in item['name']) and not '助理' in item['name']:
            continue

        # 为了能够作为key，所以将salary转化为tuple
        salary = tuple(item['salary'])

        # 名称映射
        if not name in postition_memory_db:
            postition_memory_db[name] = {}

        # 企业名映射
        if not company in postition_memory_db[name]:
            postition_memory_db[name][company] = {}

        # 地区映射
        if not location in postition_memory_db[name][company]:
            postition_memory_db[name][company][location] = {}

        # 行业映射
        if not industry in postition_memory_db[name][company][location]:
            postition_memory_db[name][company][location][industry] = {}

        # 薪资映射
        if not salary in postition_memory_db[name][company][location][industry]:
            postition_memory_db[name][company][location][industry][salary] = []

        item['company'] = item['company'].replace('拉勾未认证企业', '').strip()

        postition_memory_db[name][company][location][industry][salary].append(item)
